package app.project.streetlensproject.util;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.view.Display;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import app.project.streetlensproject.HomeActivity;
import app.project.streetlensproject.R;
import app.project.streetlensproject.model.Place;

@SuppressLint("DrawAllocation")
public class RotateCameraSurface extends View {
	Paint p;
	ArrayList<Place> arrListPlace;
	private ScaleGestureDetector mScaleDetector;
	private float mScaleFactor = 1.f;
	ArrayList<Integer> startY;
	int startRow = 3;
	int rowHeight=40;
	
	public RotateCameraSurface(Context context) {
		super(context);
		p = new Paint();
	}

	public RotateCameraSurface(Context context, ArrayList<Place> _listPlace) {
		super(context);
		startY = new ArrayList<Integer>();
		this.arrListPlace = _listPlace;
		p = new Paint();
		mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
	}

	@SuppressLint("DrawAllocation")
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		p.setColor(Color.RED);
		p.setTextSize(20);

		for (int i = 0; i < arrListPlace.size() * 50; i += 50) {
			// if(i%100==0){
			Bitmap b = getBitmapWithType(arrListPlace.get(i).categoryid);
			canvas.drawBitmap(b, 20, 100 + i, p);
			canvas.drawText(arrListPlace.get(i / 50).placename, 150, 200 + i, p);
//			float x=Float.parseFloat(String.valueOf(getCoordinatePlace(mActivity, arrListPlace.get(i), 0)));
//			float y=Float.parseFloat(String.valueOf(getCoordinatePlace(mActivity, arrListPlace.get(i), 1)));
//			canvas.drawBitmap(b,x,y, p);
//			canvas.drawText(arrListPlace.get(i / 50).placename,x,y+20, p);
			// canvas.draw
			// }
			// else{
			// canvas.drawBitmap(b, 60, 120 + i, p);
			// canvas.drawText("Text to be drwaw", 160,140+i,p);
			// }
		}
	}

	public boolean IsSpaceAvailable(Activity mActivity,
			float rotate) {
		Display localDisplay2 = mActivity.getWindowManager()
				.getDefaultDisplay();
		Point localPoint = new Point();
		localDisplay2.getSize(localPoint);
		int width = localPoint.x;
		//int height = localPoint.y;
		int mGapBetweenRects = 10;
		int bitmapWidth = 30;
		int rectWidth = 80;

		return (int) (4.0F * ((180.0F + rotate) / 360.0F * width)) >= ((Integer) startY
				.get(-1 + startY.size())).intValue()
				+ rectWidth
				+ bitmapWidth
				+ mGapBetweenRects;				
	}

	public void InsertItem(Activity mActivity, int paramInt, float rotate) {

		Display localDisplay2 = mActivity.getWindowManager()
				.getDefaultDisplay();
		Point localPoint = new Point();
		localDisplay2.getSize(localPoint);
		int width = localPoint.x;
//		int height = localPoint.y;
//		int mGapBetweenRects = 10;
		int i = (int) (4.0F * ((180.0F + rotate) / 360.0F * width));
		startY.add(Integer.valueOf(i));
		// rowInfo[paramInt].placeItem.add(paramStringList);
	}

	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		// Let the ScaleGestureDetector inspect all events.
		mScaleDetector.onTouchEvent(ev);
		final int action = ev.getAction();

		return true;
	}

	private class ScaleListener extends
			ScaleGestureDetector.SimpleOnScaleGestureListener {
		@Override
		public boolean onScale(ScaleGestureDetector detector) {
			mScaleFactor *= detector.getScaleFactor();

			// Don't let the object get too small or too large.
			mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 10.0f));

			invalidate();
			return true;
		}
	}

	private Bitmap getBitmapWithType(int type) {
		Bitmap b;
		switch (type) {
		case Constants.ID_C_BANK:
			b = BitmapFactory
					.decodeResource(getResources(), R.drawable.ic_bank);
			break;
		case Constants.ID_C_BUS:
			b = BitmapFactory
					.decodeResource(getResources(), R.drawable.ic_bank);
			break;
		case Constants.ID_C_FOOD:
			b = BitmapFactory
					.decodeResource(getResources(), R.drawable.ic_bank);
			break;
		case Constants.ID_C_FUEL:
			b = BitmapFactory
					.decodeResource(getResources(), R.drawable.ic_bank);
			break;
		case Constants.ID_C_GIAI_TRI:
			b = BitmapFactory.decodeResource(getResources(),
					R.drawable.ic_entertaiment);
			break;
		case Constants.ID_C_HOTEL:
			b = BitmapFactory.decodeResource(getResources(),
					R.drawable.ic_hotel);
			break;
		case Constants.ID_C_KHAN_CAP:
			b = BitmapFactory.decodeResource(getResources(),
					R.drawable.ic_emergency);
			break;
		case Constants.ID_C_OTHER:
			b = BitmapFactory.decodeResource(getResources(),
					R.drawable.ic_other);
			break;
		case Constants.ID_C_SCHOOL:
			b = BitmapFactory.decodeResource(getResources(),
					R.drawable.ic_school);
			break;
		case Constants.ID_C_SHOP:
			b = BitmapFactory
					.decodeResource(getResources(), R.drawable.ic_bank);
			break;
		default:
			b = BitmapFactory.decodeResource(getResources(),
					R.drawable.ic_launcher);
			break;
		}

		return b;
	}
	
	public double getCoordinatePlace(Activity mActivity,Place place,int type){
		Display localDisplay2 = mActivity.getWindowManager()
				.getDefaultDisplay();
		int height=localDisplay2.getHeight();
		int width=localDisplay2.getWidth();
		double distance=AppUtils.DistancePlace(HomeActivity.MY_LATITUDE,HomeActivity.MY_LONGITUDE,place.locationx,place.locationy);
		double distanceX=distance*(Math.cos(Double.parseDouble(String.valueOf(place.updateAngleRotate()))));
		double distanceY=distance*(Math.sin(Double.parseDouble(String.valueOf(place.updateAngleRotate()))));
		double x=width/2-distanceX;
		double y=height/2-distanceY;
		if(type==0){
			return x;
		}else{
			return y;
		}
	}

}